# models/sys_user_credentials.py
from sqlalchemy import Column, String, DateTime, Boolean, Integer, BIGINT, TEXT
from src.core.ormdb import Base
from src.core import str_utils as strU


class SysUserCredentials(Base):
    __tablename__ = "sys_user_credentials"
    __table_args__ = {'comment': '用户登录凭证表'}  # 表的注释

    # 主键：采用与 sys_user 相同的 UUID 风格
    id = Column(String(36), primary_key=True, default=lambda: strU.generate_id(), comment="凭证唯一标识ID")

    # 关联到用户主表
    user_id = Column(String(36), nullable=False, comment="用户ID，关联 sys_user.id")

    # 凭证类型与值
    credential_type = Column(String(20), nullable=False, comment="凭证类型：username、phone、email、wechat_openid、wechat_unionid、apple_id 等")
    credential_value = Column(String(200), nullable=False, comment="凭证值（加密存储）")

    # 状态标记
    verified = Column(Boolean, default=False, comment="是否已验证：1已验证，0未验证")
    is_primary = Column(Boolean, default=False, comment="是否主凭证（用于找回密码等）")
    enabled = Column(Boolean, default=True, comment="是否启用：1启用，0禁用")

    # 冗余记录最近一次登录时间（可选）
    last_login_at = Column(DateTime, nullable=True, comment="最近一次使用该凭证登录的时间")

    # 审计字段
    create_date = Column(DateTime, default=lambda: strU.now(), comment="创建时间")
    creator_by = Column(String(255), nullable=True, comment="创建者")
    last_updated_date = Column(DateTime, nullable=True, onupdate=lambda: strU.now(), comment="最后更新时间")
    last_updated_by = Column(String(255), nullable=True, comment="最后更新者")

    # 软删除
    deletion_mark = Column(Boolean, default=False, comment="删除标志：0未删除，1已删除")
    deletion_date = Column(DateTime, nullable=True, comment="删除时间")
    deletion_by = Column(String(255), nullable=True, comment="删除者")
